package com.suzhe.ymall.order.listener;

import com.suzhe.ymall.order.enums.OrderStatusEnum;
import com.suzhe.ymall.order.pojo.OrderStatus;
import com.suzhe.ymall.order.service.IMessageLogService;
import com.suzhe.ymall.order.service.IOrderService;
import com.suzhe.ymall.order.service.IOrderStatusService;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @author suzhe
 * @date 2019/8/13
 */
@Slf4j
@Component
// topic需要和生产者的topic一致，consumerGroup属性是必须指定的，内容可以随意
@RocketMQMessageListener(topic = "SCHEDULED_ORDER", consumerGroup = "order-service-schedule")
public class OrderScheduledListener implements RocketMQListener<String> {

    @Autowired
    IOrderStatusService iOrderStatusService;

    @Autowired
    IOrderService iOrderService;

    @Autowired
    IMessageLogService iMessageLogService;
    /**
     * 订单延迟消息
     * @param orderNo
     */
    @Override
    public void onMessage(String orderNo) {
        log.info("OrderScheduledListener receive msg：{}", orderNo);
        OrderStatus orderStatus = iOrderStatusService.getById(orderNo);
        if (OrderStatusEnum.INIT.value().equals(orderStatus.getStatus())){
            iOrderService.sendOrderRollbackMessage(orderNo);
        }
    }
}
